package org.example.classtopia.Controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.example.classtopia.Model.vo.ResourceListVO;
import org.example.classtopia.Model.vo.PageResultVO;
import org.example.classtopia.Model.vo.ResourceStatsVO;
import org.example.classtopia.Service.ResourceService;
import org.example.classtopia.Common.Result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/resources")
@Tag(name = "资源管理", description = "资源相关接口")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @Operation(summary = "分页获取资源列表", description = "分页获取所有正常资源，categoryId可选")
    @PostMapping("/page")
    public Result<PageResultVO<ResourceListVO>> pageList(
            @Parameter(description = "当前页码", required = true) @RequestParam Integer pageNum,
            @Parameter(description = "每页数量", required = true) @RequestParam Integer pageSize,
            @Parameter(description = "分类ID（可选）") @RequestParam(required = false) Long categoryId,
            @Parameter(description = "文件类型（可选，后缀名如 .pdf）") @RequestParam(required = false) String fileType,
            @Parameter(description = "关键词（可选）") @RequestParam(required = false) String keyword) {
        org.example.classtopia.Model.dto.ResourcePageQueryDTO queryDTO = new org.example.classtopia.Model.dto.ResourcePageQueryDTO();
        queryDTO.setPageNum(pageNum);
        queryDTO.setPageSize(pageSize);
        queryDTO.setCategoryId(categoryId);
        queryDTO.setFileType(fileType);
        queryDTO.setKeyword(keyword);
        PageResultVO<ResourceListVO> pageResult = resourceService.pageList(queryDTO);
        return Result.success(pageResult);
    }

    @Operation(summary = "分页获取指定用户的资源列表", description = "分页获取某个用户上传的所有正常资源")
    @PostMapping("/user/page")
    public Result<PageResultVO<ResourceListVO>> pageListByUser(
            @Parameter(description = "当前页码", required = true) @RequestParam Integer pageNum,
            @Parameter(description = "每页数量", required = true) @RequestParam Integer pageSize,
            @Parameter(description = "被查看的用户ID", required = true) @RequestParam Long userId) {
        org.example.classtopia.Model.dto.UserResourcePageQueryDTO queryDTO = new org.example.classtopia.Model.dto.UserResourcePageQueryDTO();
        queryDTO.setPageNum(pageNum);
        queryDTO.setPageSize(pageSize);
        queryDTO.setUserId(userId);
        PageResultVO<ResourceListVO> pageResult = resourceService.pageListByUser(queryDTO);
        return Result.success(pageResult);
    }

    @Operation(summary = "修改资源状态", description = "根据资源ID修改资源的状态（0-删除, 1-正常）")
    @PostMapping("/status/update")
    public Result<Boolean> updateResourceStatus(
            @Parameter(description = "资源ID", required = true) @RequestParam Long resourceId,
            @Parameter(description = "目标状态(0-删除, 1-正常)", required = true) @RequestParam Integer status) {
        boolean result = resourceService.updateResourceStatus(resourceId, status);
        return result ? Result.success(true) : Result.error(400, "修改失败");
    }

    @Operation(summary = "获取资源统计信息", description = "返回资源总数、贡献者数量和总下载量")
    @GetMapping("/stats")
    public Result<ResourceStatsVO> getResourceStats() {
        ResourceStatsVO stats = resourceService.getResourceStats();
        return Result.success(stats);
    }

    
}
